package pl.edu.agh.uhm.xenconnector;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import pl.edu.agh.xenapi.Connection;
import pl.edu.agh.xenapi.Network;
import pl.edu.agh.xenapi.Pool;
import pl.edu.agh.xenapi.SR;
import pl.edu.agh.xenapi.Session;
import pl.edu.agh.xenapi.VBD;
import pl.edu.agh.xenapi.VDI;
import pl.edu.agh.xenapi.VIF;
import pl.edu.agh.xenapi.VM;
import pl.edu.agh.xenapi.Types.OnCrashBehaviour;
import pl.edu.agh.xenapi.Types.OnNormalExit;
import pl.edu.agh.xenapi.Types.VbdMode;
import pl.edu.agh.xenapi.Types.VbdType;
import pl.edu.agh.xenapi.Types.VdiType;
import pl.edu.agh.xenapi.Types.VmPowerState;


public class BasicConnector {
	
    protected static Connection connection;
    public static final String CONNECTION_URL="http://localhost:9363";

  
    
    
	public List<String> getVmNames() {
		try {
			connection = new Connection(new URL(CONNECTION_URL));
			Session.loginWithPassword(connection, "any", "any");
			
			//---------SR---------
			/*Set<Pool> pools = Pool.getAll(connection);
	        Pool pool = (pools.toArray(new Pool[0]))[0];
	        SR sr = pool.getDefaultSR(connection);*/
			
	        //------VM--------
/*			VM.Record vmRecord = new VM.Record();
			
			vmRecord.nameLabel = "xen2.example.com";
			vmRecord.VCPUsAtStartup = new Long(1);
			vmRecord.VCPUsMax = new Long(1);
			vmRecord.actionsAfterShutdown = OnNormalExit.DESTROY;			
			vmRecord.actionsAfterReboot = OnNormalExit.RESTART;
			vmRecord.actionsAfterCrash = OnCrashBehaviour.RESTART;
			vmRecord.powerState = VmPowerState.HALTED; 
			vmRecord.memoryStaticMax = new Long(128*1024*1024);
			vmRecord.memoryStaticMin = new Long(0);
			vmRecord.memoryDynamicMax = new Long(128*1024*1024);
			vmRecord.PVKernel = "/boot/vmlinuz-2.6.24-27-xen";
			vmRecord.PVRamdisk = "/boot/initrd.img-2.6.24-27-xen";
			
			VM newVM = VM.create(connection, vmRecord);
			
			//--------VIF-------
	        VIF.Record vifRecord = new VIF.Record();
	        
	        Set<Network> networks = Network.getAll(connection);
	        Network network = null;
	        for (Network n : networks){
	        	network = n;
	        	break;
	        }

	        vifRecord.VM = newVM;
	        vifRecord.network = network;
	        vifRecord.device = "0";
	        vifRecord.MTU = 1500L;

	        VIF.create(connection, vifRecord);
	        
	        //--------VBD---------
		*/	
			/*VBD.Record vbdRecord1 = new VBD.Record();
			vbdRecord1.bootable = true;
			vbdRecord1.device = "/dev/xvda1";
			vbdRecord1.mode = VbdMode.RO;
			vbdRecord1.type = VbdType.DISK;
			
			
			VDI.Record vdiRecord1 = new VDI.Record();
			vdiRecord1.location = "tap:aio:/home/xen/domains/xen1.example.com/swap.img,xvda1";
			vdiRecord1.readOnly = false;*/
	/*		
			VDI.Record vdiRecord2 = new VDI.Record();
			vdiRecord2.location = "tap:aio:/home/xen/domains/xen1.example.com/disk.img,xvda2";
			vdiRecord2.readOnly = false;
			vdiRecord2.type = VdiType.SYSTEM;
			VDI vdi = VDI.create(connection, vdiRecord2);
			
			VBD.Record vbdRecord2 = new VBD.Record();
			vbdRecord2.VM = newVM;
			vbdRecord2.VDI = vdi;
			vbdRecord2.bootable = false;
			vbdRecord2.device = "/dev/xvda2";
			vbdRecord2.mode = VbdMode.RO;
			vbdRecord2.type = VbdType.DISK;
			VBD vbd = VBD.create(connection, vbdRecord2);
					
			newVM.provision(connection);
	       */ 
			List<String> result=new ArrayList<String>();
			Set<VM> allVMs = VM.getAll(connection);
			
			for(VM vm : allVMs){
				VM.Record tmpVmRecord = vm.getRecord(connection);
				result.add(tmpVmRecord.uuid);
				if(tmpVmRecord.isControlDomain)
					System.out.println("Dom0: " + tmpVmRecord.uuid);
				else{
					System.out.println("DomU: " + tmpVmRecord.uuid);
					//vm.start(connection, false, false);
				}
			}
			
	        Session.logout(connection);
	        return result;
	        
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return null;
	}
}
